package com.xing.book.view;

import com.xing.book.domin.Book;
import com.xing.book.domin.User;
import com.xing.book.service.BookService;
import com.xing.book.service.UserService;
import com.xing.book.service.impl.BookServiceImpl;
import com.xing.book.service.impl.UserServiceImpl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Scanner;

public class BookSystem {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = input.next();
        System.out.println("请输入密码");
        String password = input.next();
        UserService userService=new UserServiceImpl();

        boolean flag=true;
        try {
            User login = userService.login(username, password);
            System.out.println("登录成功...欢迎:"+login.getUsername());
            do {
                //alt+enter:自动修复（万能键）
                //书籍业务对象
                BookService bookService=new BookServiceImpl();
                System.out.println("======1.查询所有 2.根据书名查询 3.添加 4.修改 5.删除 0.退出========");
                System.out.println("请选择：");
                int choice = input.nextInt();
                switch (choice){
                    case 1:
                       List<Book> books=bookService.findAll();
                        printBook(books);
                        break;
                    case 2:
                        System.out.println("请输入要查询的书名");
                        String title = input.next();
                        List<Book> books2 = bookService.findByTitle(title);
                        printBook(books2);
                        break;
                    case 3:
                        Book book=getBook();
                        if(book!=null){
                            bookService.add(book);
                            System.out.println("添加成功");
                        }
                        break;
                    case 4:
                        //修改
                        //1 根据id 查询图书
                        System.out.println("请输入更新书籍的id");
                        int bid= input.nextInt();
                        Book bk=bookService.findById(bid);
                        //2.更新
                        if(bk!=null){
                        System.out.println("请输入新的书名");
                        String newTitle=input.next();
                        System.out.println("请输入新的价格");
                        double newPrice=input.nextDouble();
                        bk.setTitle(newTitle);
                        bk.setPrice(new BigDecimal(newPrice));

                        try {
                            bookService.modify(bk);
                            System.out.println("修改成功");
                        } catch (Exception e) {
                            System.out.println(e.getMessage());
                        }}else {
                            System.out.println("书籍不存在!");
                        }

                        break;
                    case 5:
                        System.out.println("请输入删除书籍的id");
                        int bookId=input.nextInt();
                        try {
                            bookService.remove(bookId);
                            System.out.println("删除成功");
                        }catch (Exception e){
                            System.out.println(e.getMessage());
                        }
                        break;
                    case 0:
                        flag=false;
                        System.out.println("谢谢使用...欢迎下次光临");
                        break;

                    default:
                        System.out.println("输入有误，请重新输入");
                        break;
                }
            } while (flag);
        } catch (Exception e) {
            System.out.println("失败"+e.getMessage());
        }
        System.out.println("欢迎下次光临");
    }

    public static void printBook(List<Book> books){
        if(books!=null&&books.size()>0){
            for (Book book : books) {
                System.out.println(book);
            }
        } else {
            System.out.println("没有查到数值");
        }
    }

    public static Book getBook(){
        Scanner input = new Scanner(System.in);
        System.out.println("请输入书名");
        String title = input.next();

        System.out.println("请输入作者");
        String author = input.next();
        
        System.out.println("请输入出版时间");
        String publicDate = input.next();

        System.out.println("请输入出版社");
        String publisher = input.next();

        System.out.println("请输入书名isbn");
        String isbn = input.next();
        System.out.println("请输入书名价格");
        String price = input.next();
        System.out.println("请输入书名类别");
        int cid = input.nextInt();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        try {
            Book book= new Book(0,title,author,sdf.parse(publicDate),publisher,isbn,new BigDecimal(price),null,cid);
            return book;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;


    }
}
